package com.zxbit.flash.presenter;

import android.annotation.SuppressLint;

import com.zxbit.flash.BuildConfig;
import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.DictionaryMvpView;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.bean.DictionaryItemData;
import com.zxbit.flash.response.DictionaryResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

@SuppressLint("CheckResult")
public class DictionaryPresenter extends BasePresenter {
    private DictionaryMvpView mvpView;

    public DictionaryPresenter(DictionaryMvpView mvpView) {
        this.mvpView = mvpView;
    }

    public void getDictionaryData(List<String> types) {
        List<Observable<DictionaryResponse>> observableList = new ArrayList<>();
        for (String type : types) {
            Map<String, Object> formMap = new HashMap<>();
            formMap.put("dictType", type);
            observableList.add(req.getDictionaryData(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(formMap)));
        }

        Observable.zip(observableList, new Function<Object[],  ArrayList<ArrayList<DictionaryItemData>>>() {
            @Override
            public  ArrayList<ArrayList<DictionaryItemData>> apply(Object[] objects) throws Exception {
                ArrayList<ArrayList<DictionaryItemData>>responseArr=new ArrayList<>();
                for (int i=0 ;i<objects.length;i++) {
                    DictionaryResponse response = (DictionaryResponse) objects[i];
                    responseArr.add(response.getData());
                }
                return responseArr;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Consumer<ArrayList<ArrayList<DictionaryItemData>>>() {

                    @Override
                    public void accept(ArrayList<ArrayList<DictionaryItemData>> arrayLists) throws Exception {
                        mvpView.onGetDataNext(arrayLists);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mvpView.onGetDataError(throwable);
                    }
                });
    }

    /**
     * 获取字典不带渠道号
     * @param types
     */
    public void getDictionaryDataNoChannel(List<String> types) {
        List<Observable<DictionaryResponse>> observableList = new ArrayList<>();
        for (String type : types) {
            Map<String, Object> formMap = new HashMap<>();
            formMap.put("dictType", type);
            observableList.add(req.getDictionaryData(UserInfo.getToken(), ParamsFactory.getCommonParams(formMap)));
        }

        Observable.zip(observableList, new Function<Object[],  ArrayList<ArrayList<DictionaryItemData>>>() {
            @Override
            public  ArrayList<ArrayList<DictionaryItemData>> apply(Object[] objects) throws Exception {
                ArrayList<ArrayList<DictionaryItemData>>responseArr=new ArrayList<>();
                for (int i=0 ;i<objects.length;i++) {
                    DictionaryResponse response = (DictionaryResponse) objects[i];
                    responseArr.add(response.getData());
                }
                return responseArr;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Consumer<ArrayList<ArrayList<DictionaryItemData>>>() {

                    @Override
                    public void accept(ArrayList<ArrayList<DictionaryItemData>> arrayLists) throws Exception {
                        mvpView.onGetDataNext(arrayLists);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mvpView.onGetDataError(throwable);
                    }
                });
    }
}
